package com.ztr.tree.twotree;

import java.util.Stack;

public class TreeNode {
    public char val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(char val) {
        this.val = val;
    }

    @Override
    public String toString() {
        return this.val+"";
    }


    public void preOrder1(TreeNode root){
        if(root==null){
            return ;
        }
        System.out.println(root.val);
        preOrder1(root.left);
        preOrder1(root.right);
    }

    public void preOrder2(TreeNode root){
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(!stack.empty()||cur!=null){
            if(cur!=null){
                //打印根节点
                System.out.print(cur.val+" ");
                //根节点入栈
                stack.push(cur);
                //访问左子树
                cur=cur.left;
            }
            else{
                cur=stack.pop().right;
            }
        }
    }
}
